﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WoChu.Infrastructure.Data;
using WoChu.Infrastructure.Dapper;
using WoChu.WMS.Entity;

namespace WoChu.WMS.Repository
{
    /// <summary>
    /// 订单相关视频记录
    /// </summary>
    public class SaleAPPVideoRepository : RepositoryBase<SaleAPPVideo, string>
    {
        public SaleAPPVideoRepository() : base(ConnectionsDBKey.ERP_Ora) { }

        public int Add(SaleAPPVideo entity)
        {
            string sql = @"
            INSERT INTO SaleAPPVideo
            (ID, SHEETID, FILENAME, TYPE, SUBTYPE, DURATION, CREATEUSER, CREATETIME)
            values
            (saleappvideo_sequence.nextval, :SHEETID, :FILENAME, :TYPE, :SUBTYPE, :DURATION, :CREATEUSER, sysdate)
            ";           

            int ret = Execute(sql, entity);
            return ret;   
        }

        public int Update(SaleAPPVideo entity)
        {
            string sql = @"
            Update SaleAPPVideo Set SHEETID=:SHEETID, FILENAME=:FILENAME, TYPE=:TYPE, SUBTYPE=:SUBTYPE, DURATION=:DURATION, CREATEUSER=:CREATEUSER, CREATETIME=:CREATETIME
            Where ID=:ID
            ";

            int ret = Execute(sql, entity);
            return ret;   
        }

        public int Exists(SaleAPPVideo entity)
        {
            string sql = @"
            Select ID From SaleAPPVideo 
            Where SHEETID=:SHEETID And TYPE=:TYPE And SUBTYPE=:SUBTYPE And DURATION=:DURATION            
            ";

            int ret = ExecuteScalar<int>(sql, entity);
            return ret;   
        }

        public int Delete(SaleAPPVideo entity)
        {
            StringBuilder sql = new StringBuilder(100);
            StringBuilder whereSql = new StringBuilder(50);
            sql.Append(@"
            delete from SaleAPPVideo where 1=1
            ");
            if (entity.ID>0)
                whereSql.Append(" And ID=:ID");
            if(!string.IsNullOrEmpty(entity.FILENAME))
                whereSql.Append(" And filename=:filename");

            sql.Append(whereSql);
            int ret = Execute(sql.ToString(), entity);
            return ret;
        }

        public List<SaleAPPVideo> GetVideoList(SaleAPPVideo condition)
        {
            StringBuilder sql = new StringBuilder();
            StringBuilder whereSql = new StringBuilder();
            sql.Append(@"
            Select ID, SHEETID, FILENAME, TYPE, SUBTYPE, DURATION, CREATEUSER, CREATETIME
            From asf.SaleAPPVideo
            Where 1=1
            ");


            if (!string.IsNullOrEmpty(condition.SHEETID))
                whereSql.Append(" And SHEETID=:SHEETID");
            if (condition.TYPE > 0)
                whereSql.Append(" And TYPE=:TYPE");
            if (condition.SUBTYPE > 0)
                whereSql.Append(" And SUBTYPE=:SUBTYPE");
            sql.Append(whereSql);

            var list = Query(sql.ToString(), condition);
            return list;
        }

        
    }
}
